﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;

namespace Hinacom.Scheduler.Configuration
{
    /// <summary>
    /// Scheduler配置。
    /// </summary>
    public class SchedulerConfiguration: ConfigurationSection
    {
        private static System.Configuration.Configuration separatedConfig = null;
        /// <summary>
        /// 当前的Scheduler配置。若当前的应用配置（app.config、web.config等）中包含Scheduler配置，使用此配置；否则查找scheduler.config中的配置。
        /// </summary>
        public static SchedulerConfiguration Current
        {
            get
            {
                // 当前应用配置
                object sct = ConfigurationManager.GetSection("scheduler") as SchedulerConfiguration;
                if (sct != null)
                    return (SchedulerConfiguration)sct;
                // 查找scheduler.config
                if (separatedConfig == null)
                {
                    string separatedConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "scheduler.config");
                    if (File.Exists(separatedConfigPath))
                    {
                        ExeConfigurationFileMap map = new ExeConfigurationFileMap() { ExeConfigFilename = separatedConfigPath };
                        separatedConfig = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                    }
                }

                if (separatedConfig != null)
                    return separatedConfig.GetSection("scheduler") as SchedulerConfiguration;

                return null;
            }
        }
        /// <summary>
        /// 任务类型配置。
        /// </summary>
        [ConfigurationProperty("jobtypes", IsRequired = false)]
        [ConfigurationCollection(typeof(JobTypeConfigurationCollection), AddItemName = "jobtype")]
        public JobTypeConfigurationCollection JobTypes
        {
            get { return this["jobtypes"] as JobTypeConfigurationCollection; }
        }

    }
}
